﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CaptchaLearning.Infrastructure;
using CaptchaLearning.Infrastructure.Algorithm;
using CaptchaLearning.Infrastructure.Service;

namespace CaptchaLearning.Setting
{
    public class SettingViewModel : WorkspaceViewModel
    {
        #region Fields

        private IXmlService _xmlService;

        private Infrastructure.Model.AlgorithmOption _algorithm;

        private ObservableCollection<string> _characters;

        private string _character;

        private string _noPixels;

        private string _tolerances;

        private string _iterations;

        #endregion // Fields

        #region Properties

        public Infrastructure.Model.AlgorithmOption Algorithm
        {
            get { return _algorithm; }
            set
            {
                _algorithm = value;

                Global.CurrentProject.AlgorithmOption = _algorithm;

                switch (_algorithm)
                {
                    case Infrastructure.Model.AlgorithmOption.Backward_Forward:
                        Global.CurrentProject.Algorithm = new BaumWelchLearning();
                        break;
                    case Infrastructure.Model.AlgorithmOption.Maximum_Entropy_Markov_Model:
                        break;
                }

                OnPropertyChanged("Algorithm");
            }
        }

        public ObservableCollection<string> Characters
        {
            get { return _characters; }
            set { _characters = value; OnPropertyChanged("Characters"); }
        }

        public string Character
        {
            get { return _character; }
            set { _character = value; OnPropertyChanged("Character"); }
        }

        public string NoPixels
        {
            get { return _noPixels; }
            set 
            {
                _noPixels = value;
                Global.CurrentProject.Pixels = int.Parse(_noPixels);
                OnPropertyChanged("NoPixels"); 
            }
        }

        public string Tolerances
        {
            get { return _tolerances; }
            set 
            { 
                _tolerances = value;
                Global.CurrentProject.Tolerance = int.Parse(_tolerances);
                OnPropertyChanged("Tolerances"); 
            }
        }

        public string Iterations
        {
            get { return _iterations; }
            set 
            { 
                _iterations = value;
                Global.CurrentProject.Iterations = int.Parse(_iterations);
                OnPropertyChanged("Iterations"); 
            }
        }

        #endregion // Properties

        #region Constructor

        public SettingViewModel()
        {
            DisplayName = "Setting";
            this.Characters = Global.Characters;
            this._xmlService = new XmlService();

            this._exportCommand = new DelegateCommand(new Action(ExecuteExportCommand), () => true);
            this._importCommand = new DelegateCommand(new Action(ExecuteImportCommand), () => true);

            Initialize();
        }

        public void Initialize()
        {
            _noPixels = Global.CurrentProject.Pixels.ToString();
            _iterations = Global.CurrentProject.Iterations.ToString();
            _tolerances = Global.CurrentProject.Tolerance.ToString();
        }

        #endregion // Constructor

        #region Commands

        private DelegateCommand _exportCommand;
        public DelegateCommand ExportCommand { get { return _exportCommand; } }
        public void ExecuteExportCommand()
        {
            FolderBrowserDialog open = new FolderBrowserDialog();
            if(open.ShowDialog() == DialogResult.OK)
            {
                string folder = open.SelectedPath;
                _xmlService.SaveProject(Global.CurrentProject, Path.Combine(folder, "demo.hmm"));
            }
        }

        private DelegateCommand _importCommand;
        public DelegateCommand ImportCommand { get { return _importCommand; } }
        public void ExecuteImportCommand()
        {
            OpenFileDialog fdlgOpen = new OpenFileDialog();
            fdlgOpen.Filter = "project file (*.hmm)|*.hmm";
            if (fdlgOpen.ShowDialog() == DialogResult.OK)
            {
                string file = fdlgOpen.FileName;
                Global.CurrentProject = _xmlService.LoadProject(file);
                Algorithm = Global.CurrentProject.AlgorithmOption;
                NoPixels = Global.CurrentProject.Pixels.ToString();
                Tolerances = Global.CurrentProject.Tolerance.ToString();
                Iterations = Global.CurrentProject.Iterations.ToString();
            }
        }

        #endregion // Commands
    }
}
